package com.jim.study.delicacy_bjx.controller;

import com.alibaba.fastjson.JSONObject;
import com.jim.study.delicacy_bjx.dao.UserMapper;
import com.jim.study.delicacy_bjx.entity.Preference;
import com.jim.study.delicacy_bjx.entity.User;
import com.jim.study.delicacy_bjx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@CrossOrigin
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    /**
     * 默认路由到登录页面
     * @return
     */
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ModelAndView comeToLogin(){
        return new ModelAndView("login");
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Object Login(@RequestParam("username") String username,
                        @RequestParam("password") String password){
        User user = userMapper.getUser(username, password);
        if(user==null){
            return null;
        }
        else return user;
    }

    /**
     * 注册
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/sign",method = RequestMethod.POST)
    public Object signUp(@RequestParam("regname") String username,
                         @RequestParam("regpass") String password){
        if(username.isEmpty()||password.isEmpty()){
            return "failed";
        }
        else {
            return userService.insertUser(username,password);
        }
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @RequestMapping(value = "/changeUser",method = RequestMethod.POST)
    public String changeUser(User user){
        userService.updateUser(user);
        return "success";
    }

    /**
     * 进入首页
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView comeToIndex(Model model,@RequestParam("username") String username){
        model.addAttribute("username",username);
        return new ModelAndView("index");
    }

    /**
     * 进入推荐
     * @return
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.GET)
    public ModelAndView comeToRecommend(Model model,@RequestParam("username") String username){
        model.addAttribute("username",username);
        return new ModelAndView("recommend");
    }

    /**
     * 进入偏好设置
     * @return
     */
    @RequestMapping(value = "/preference", method = RequestMethod.GET)
    public ModelAndView comeToPreference(Model model,@RequestParam("username") String username){
        List<Preference> preferences = userMapper.getPreferenceByUsername(username);
        model.addAttribute("username",username);
        model.addAttribute("preferences",preferences);
        return new ModelAndView("preference");
    }

    /**
     * 修改偏好
     * @param data
     * @return
     */
    @RequestMapping(value = "/insertPreference",method = RequestMethod.POST)
    public String insertPreference(@RequestBody JSONObject data){
        userService.insertPreference(data);
        return "success";
    }
}
